(function (window, undefined) {
    if(!CLASS){
        var CLASS = {
            URL: ['PatientData', 'PatientGuarder', 'PatientClinical', 'PatientCover', 'PatientPhoto', 'PatientXRay', 'PatientMouthScan'],
            imgList: '.imgage',
            imgageUpload: '.upload',
            next: '.next-step',
            pre: '.pre-step',
            formHd: '.message-nav .item'
        }
    }
    if(!getDom){
        var getDom = function () {
            return {
                img_list: document.querySelectorAll(CLASS.imgList),
                imgage_upload: document.querySelectorAll(CLASS.imgageUpload),
                next_btn: document.querySelector(CLASS.next),
                pre_btn: document.querySelector(CLASS.pre),
                form_hd_btn: document.querySelectorAll(CLASS.formHd)
            }
        }
    }
    var dom;
    var messageHandle = function (opt) {
        this.config(opt);
        dom = getDom();
        this.init();
    }
    messageHandle.prototype = {
        constructor: messageHandle,
        config: function (opt) {
            if(!opt) return CLASS;
            for(var key in opt) {
                CLASS[key] = opt[key];
            }
            return this;
        },
        init: function () {
            var _that = this;
            dom.imgage_upload && (dom.imgage_upload.forEach((item, index) => item.onchange = function () {
                _that.imgageUpload(item, index)
            }));
            dom.next_btn && (dom.next_btn.onclick = function () {
                _that.nextStep();
            });
            dom.pre_btn && (dom.pre_btn.onclick = function () {
                _that.preStep();
            });
            dom.form_hd_btn && (dom.form_hd_btn.forEach((item, index) => item.onclick = function () {
                _that.skip(item, index)
            }));
        },
        imgageUpload: function (item, index) {
            var image = item.files[item.files.length - 1];
            var url = window.URL.createObjectURL(image);
            dom.img_list[index].src = url; 
        },
        post: function (url, obj) {
            return axios.post('', {
                params: obj
            })
            .then(response => {
                console.log(response)
                return response.data
                
            })
            .catch(e => console.log(e));
        },
        getUrlIndex: function () {
            var url = CLASS.URL;
            for (var i = 0; i < url.length; i++) {
                 if (~window.location.pathname.indexOf(url[i])) {
                     break;
                 }
            }
            return i;
        },
        nextStep: function () {
            window.location.href = CLASS.URL[this.getUrlIndex() + 1] + '.html';
        },
        preStep: function () {
            window.location.href = CLASS.URL[this.getUrlIndex() - 1] + '.html';
        },
        skip: function () {
            window.location.href = CLASS.URL[arguments[1]] + '.html';
        }
    }
    !window.messageHandle && (window.messageHandle = messageHandle)
})(window)